#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

/*#define ITEM_FAD_LEFT		(0)
#define ITEM_FAD_UP		(1)
#define ITEM_FAD_CENTER		(2)
#define ITEM_FAD_DOWN		(3)
#define ITEM_FAD_RIGHT		(4)
#define ITEM_FAD_END		(5)
*/
#define ITEM_FAD_UP		(0)
#define ITEM_FAD_DOWN		(1)
#define ITEM_FAD_LEFT		(2)
#define ITEM_FAD_RIGHT		(3)
#define ITEM_FAD_CENTER		(4)
#define ITEM_FAD_END		(5)
#define FOCUS_DISTANCE       22

TRect		AudioFADTouchRect[IMG_AUDIO_FAD_ITEM_QTY];
TPosCoor	AudioFADItemPos[IMG_AUDIO_FAD_ITEM_QTY] = { {156,80}, {156,265}, {49,172}, {286,172}, {156,172}, {576,375} };
TPosCoor	AudioFADBarPos = {396,80};
TPosCoor	AudioFADFocusPos = {405,89};
int		AudioFADFocus = -1;
TFrame		AudioFADFrame;
//unsigned char FAD_data [5] = {0xe0,0x03,0x06,0x0e,0xF7};
extern  unsigned char send_data_buf[5];
char*		AudioFADInfo = { "END"};
static int FADValue =0;
static int BALValue =0;
extern	unsigned char read_info_data[10];
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;

int  FADInfoUpdata (void)
{
	 
	switch (read_info_data[3])
		{
			case	ID_SCREEN_INFO_BAL:
				BALValue = read_info_data[4] ;
				break;		
			case  ID_SCREEN_INFO_FAD:
				FADValue = read_info_data[4] ;
				break;
			}
			return 0;
}


void AudioFADDrawBackgroup(TCanvas *pCanvas)
{	
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "FAD/BAL SET" );
}


void   AudioFADDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}	
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "FAD/BAL SET" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				 AudioFADDrawVolume(AudioFADFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void AudioFADDrawBar( TCanvas *pCanvas )
{
	TJpeg pJpeg;
	TRect rect;
	char FileName[20];
	TPosCoor	AudioFADCurPos;
	int GetJpegIdx;	
	int i,j;
	
	GetJpegIdx = IMG_AUDIO_FAD_BARCOVER;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_FAD_FOLDER, GetJpegIdx);		
	
	pJpeg = GetJpegPtr(FileName);
	rect.left =  AudioFADBarPos.x;
	rect.right = AudioFADBarPos.x + pJpeg.Width;
	rect.top = AudioFADBarPos.y;
	rect.bottom = AudioFADBarPos.y + pJpeg.Height;
	DrawJpeg(pCanvas, AudioFADBarPos.x, AudioFADBarPos.y, FileName);

	AudioFADCurPos.x = AudioFADFocusPos.x + FOCUS_DISTANCE *(BALValue-1);
	AudioFADCurPos.y = AudioFADFocusPos.y + FOCUS_DISTANCE *(FADValue-1);
	AudioFADCurPos.x = ((AudioFADCurPos.x < rect.left )?rect.left : \
							(AudioFADCurPos.x > rect.right )?rect.right: AudioFADCurPos.x );
	AudioFADCurPos.y = ((AudioFADCurPos.y < rect.top )?rect.top : \
							(AudioFADCurPos.y > rect.bottom)?rect.bottom: AudioFADCurPos.y );
	GetJpegIdx = IMG_AUDIO_FAD_BAR;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_FAD_FOLDER, GetJpegIdx);
	DrawJpeg(pCanvas, AudioFADCurPos.x, AudioFADCurPos.y, FileName);
	dt_Present_part(rect);
	printf ("AudioFADCurPosx :%d,y: %d,rect x:%d,y:%d\n",AudioFADCurPos.x,AudioFADCurPos.y,rect.left,rect.top);
	
}


void AudioFADDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= IMG_AUDIO_FAD_ITEM_QTY )
		return;

	if( bfocus ){
		GetJpegIdx = IMG_AUDIO_FAD_ITEM_FOCUS_UP+idx ;
	}else{
		GetJpegIdx = IMG_AUDIO_FAD_ITEM_UP+idx ;
	}
	pos = AudioFADItemPos[idx];

	sprintf( FileName, "res_jpeg/%d/%d.jpg", AUDIO_FAD_FOLDER, GetJpegIdx);	
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );	

	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	
	AudioFADTouchRect[idx].left = pos.x;
	AudioFADTouchRect[idx].right = pos.x + pJpeg.Width;
	AudioFADTouchRect[idx].top = pos.y;
	AudioFADTouchRect[idx].bottom = pos.y + pJpeg.Height;
	if (idx == ITEM_FAD_END)
	g_Puts( pCanvas, FONT33, AudioFADTouchRect[idx], TEXT_ALIGN_CENTER, AudioFADInfo );	

	
}

void AudioFADDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		AudioFADDrawBackgroup(AudioFADFrame.pCanvas);
	for( i = 0; i < IMG_AUDIO_FAD_ITEM_QTY; i++ )
		AudioFADDrawItem( AudioFADFrame.pCanvas, i, FALSE );
	AudioFADDrawBar(AudioFADFrame.pCanvas);
		if (mute_status == 1)
		{
			AudioFADDrawVolume(AudioFADFrame.pCanvas,1);	
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);	
}

int AudioFADGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_AUDIO_FAD_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, AudioFADTouchRect[i] ) )
			return i;
	return -1;
}

BOOL AudioFADBtnEnter(int idx)
{
	AudioFADDrawItem( AudioFADFrame.pCanvas, idx, FALSE );
	switch(idx)
	{
		case ITEM_FAD_LEFT:
		case ITEM_FAD_UP:
		case ITEM_FAD_CENTER:
		case ITEM_FAD_DOWN:
		case ITEM_FAD_RIGHT:
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			dt_Present_part(AudioFADTouchRect [idx]);
			//dt_Present();
			break;

		case ITEM_FAD_END:
			MenuAudioInit();
			break;
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}

void AudioFADInit()
{
	ir_SetCurrentID( IR_AUDIO_FAD_INIT );

	frm_Create( &AudioFADFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	AudioFADDraw( TRUE );
}


/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
											MenuMediaInit();
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}
									rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
																									
						break;
						
					case	ID_KEY_STATUS:						
						break;
						
					case	ID_SCREEN_INFO:
						switch (read_info_data[3])
							{
							case ID_SCREEN_INFO_VOLUME:								
								AudioFADDrawVolume(AudioFADFrame.pCanvas,1);	
								break;
							case  ID_SCREEN_INFO_BAL:
							case ID_SCREEN_INFO_FAD:
								AudioFADDrawBar(AudioFADFrame.pCanvas);
								break;								
							}
							break;						
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:	
										AudioFADDrawVolume(AudioFADFrame.pCanvas,1);
										break;
									case	ID_DMUTE:	
										AudioFADDrawVolume(AudioFADFrame.pCanvas,3);											
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
										break;
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
									case 	ID_FMAM:
										//band command;
										break;
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}		
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;		 
						break;
						
						case 	ID_BAND_SEL:
							MenuRadioInit();
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;
						break;	
						
					default:	
							rc = -1;						
						}
	return 0;					
}
/*
static int send_data_out ( int index)
{
	if ((index < 0) || (index > IMG_AUDIO_FAD_ITEM_QTY))
		return -1;
	
			if (index == ITEM_FAD_END)
			{
				FAD_data [2] = 0x07;
				FAD_data [3] = 0x01;
				FAD_data [4] = 0xeb;
			}
			else
			{	
				switch(index)
				{
					case ITEM_FAD_LEFT:
						FAD_data [2] = 0x06;
						FAD_data [3] = 0x10;
						FAD_data [4] = 0xF9;
						break;
					case ITEM_FAD_DOWN:
						FAD_data [2] = 0x06;
						FAD_data [3] = 0x0e;
						FAD_data [4] = 0xF7;
						break;
					case ITEM_FAD_CENTER:
						FAD_data [2] = 0x06;
						FAD_data [3] = 0x12;
						FAD_data [4] = 0xFB;
						break;
					case ITEM_FAD_UP:
						FAD_data [2] = 0x06;
						FAD_data [3] = 0x0f;
						FAD_data [4] = 0xF8;
						break;
					case ITEM_FAD_RIGHT:
						FAD_data [2] = 0x06;
						FAD_data [3] = 0x11;
						FAD_data [4] = 0xFA;
						break;
				}
			}
		if (dev_SPIWrite(FAD_data,5)==0)
			return 0;//hangsheng 071126		
	return -1;
}
*/
static int send_data ( int index)
{
	if ((index < 0) || (index > IMG_AUDIO_FAD_ITEM_QTY))
		return -1;
	
			if (index == ITEM_FAD_END)
			{
				send_data_buf [2] = IDDATA_ESC;
				send_data_buf [3] = ESC_BUTTON;
			}
			else
			{	
				send_data_buf [2] = IDDATA_AUDIO;

				switch(index)
				{
					
					case ITEM_FAD_LEFT:
						send_data_buf [3] = AUDIO_FAD_LEFT;
						break;
					case ITEM_FAD_DOWN:
						send_data_buf [3] = AUDIO_FAD_UP;
						break;
					case ITEM_FAD_CENTER:
						send_data_buf [3] = AUDIO_FAD_CENTER;
						break;
					case ITEM_FAD_UP:
						send_data_buf [3] = AUDIO_FAD_DOWN;
						break;
					case ITEM_FAD_RIGHT:
						send_data_buf [3] = AUDIO_FAD_RIGHT;
						break;
				}
			}
			
		send_data_buf[0] =  HEAD;		
		send_data_buf[1] =  LENGTH;		
		return ( send_data_out(send_data_buf,ALL_LENGTH) );
		
	return -1;
}


void AudioFADInput(UInt32 key)
{
	int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( AudioFADFocus = AudioFADGetTouchID( pos )) != -1 )
				{
					AudioFADDrawItem( AudioFADFrame.pCanvas, AudioFADFocus, TRUE );					
					dt_Present_part(AudioFADTouchRect [AudioFADFocus]);
					if ( send_data(AudioFADFocus))
					printf ("write failure !\n");	
				}
			}
		}
		else
		{
			if( ( focus = AudioFADGetTouchID( pos ) ) == AudioFADFocus )
				AudioFADBtnEnter( AudioFADFocus );
			else if( AudioFADFocus != -1 )
			{
				AudioFADDrawItem( AudioFADFrame.pCanvas, AudioFADFocus, FALSE );
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				dt_Present_part(AudioFADTouchRect [AudioFADFocus]);
				//dt_Present();
			}
			AudioFADFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

